package com.yc.music.exception;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.music.common.context.YcContext;

/**
 * 全局异常处理类
 * ClassName: YCExceptionHandler <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2016年11月18日  <br/>
 *
 * @author qinhaiguan
 * @version 
 */
@ControllerAdvice
public class YCExceptionHandler {
	
	private static Logger logger = Logger.getLogger(YCExceptionHandler.class);
	
	//运行时异常
    @ExceptionHandler(RuntimeException.class)  
    @ResponseBody  
    public Map<String,Object> runtimeExceptionHandler(RuntimeException runtimeException) {  
    	Map<String,Object> result = new HashMap<String,Object>();
		result.put(YcContext.CODE, 500);
		result.put(YcContext.MESSAGE, ReturnFormat.retParam(1000, null));
		result.put(YcContext.DATA,YcContext.EMPTY_STRING);
		logger.error(runtimeException);
		return result;
    }  

    //空指针异常
    @ExceptionHandler(NullPointerException.class)  
    @ResponseBody  
    public Map<String,Object> nullPointerExceptionHandler(NullPointerException ex) {  
        ex.printStackTrace();
        Map<String,Object> result = new HashMap<String,Object>();
		result.put(YcContext.CODE, 500);
		result.put(YcContext.MESSAGE, ReturnFormat.retParam(1001, null));
		result.put(YcContext.DATA,YcContext.EMPTY_STRING);
		logger.error(ex);
		return result;
    }
    
    //类型转换异常
    @ExceptionHandler(ClassCastException.class)  
    @ResponseBody  
    public Map<String,Object> classCastExceptionHandler(ClassCastException ex) {  
        ex.printStackTrace();
        Map<String,Object> result = new HashMap<String,Object>();
		result.put(YcContext.CODE, 500);
		result.put(YcContext.MESSAGE, ReturnFormat.retParam(1002, null));
		result.put(YcContext.DATA,YcContext.EMPTY_STRING);
		logger.error(ex);
		return result;
    }  

    //IO异常
    @ExceptionHandler(IOException.class)  
    @ResponseBody  
    public Map<String,Object> iOExceptionHandler(IOException ex) {  
        ex.printStackTrace();
        Map<String,Object> result = new HashMap<String,Object>();
		result.put(YcContext.CODE, 500);
		result.put(YcContext.MESSAGE, ReturnFormat.retParam(1003, null));
		result.put(YcContext.DATA,YcContext.EMPTY_STRING);
		logger.error(ex);
		return result;
    }  
    //未知方法异常
    @ExceptionHandler(NoSuchMethodException.class)  
    @ResponseBody  
    public Map<String,Object> noSuchMethodExceptionHandler(NoSuchMethodException ex) {  
        ex.printStackTrace();
        Map<String,Object> result = new HashMap<String,Object>();
		result.put(YcContext.CODE, 500);
		result.put(YcContext.MESSAGE, ReturnFormat.retParam(1004, null));
		result.put(YcContext.DATA,YcContext.EMPTY_STRING);
		logger.error(ex);
		return result;
    }  

    //数组越界异常
    @ExceptionHandler(IndexOutOfBoundsException.class)  
    @ResponseBody  
    public Map<String,Object> indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException ex) {  
        ex.printStackTrace();
        Map<String,Object> result = new HashMap<String,Object>();
		result.put(YcContext.CODE, 500);
		result.put(YcContext.MESSAGE, ReturnFormat.retParam(1005, null));
		result.put(YcContext.DATA,YcContext.EMPTY_STRING);
		logger.error(ex);
		return result;
    }
    //400错误
    @ExceptionHandler({HttpMessageNotReadableException.class})
    @ResponseBody
    public Map<String,Object> requestNotReadable(HttpMessageNotReadableException ex){
        ex.printStackTrace();
        Map<String,Object> result = new HashMap<String,Object>();
		result.put(YcContext.CODE, 400);
		result.put(YcContext.MESSAGE, ReturnFormat.retParam(400, null));
		result.put(YcContext.DATA,YcContext.EMPTY_STRING);
		logger.error(ex);
		return result;
    }
    //400错误
    @ExceptionHandler({TypeMismatchException.class})
    @ResponseBody
    public Map<String,Object> requestTypeMismatch(TypeMismatchException ex){
        ex.printStackTrace();
        Map<String,Object> result = new HashMap<String,Object>();
		result.put(YcContext.CODE, 400);
		result.put(YcContext.MESSAGE, ReturnFormat.retParam(400, null));
		result.put(YcContext.DATA,YcContext.EMPTY_STRING);
		logger.error(ex);
		return result;
    }
    //400错误
    @ExceptionHandler({MissingServletRequestParameterException.class})
    @ResponseBody
    public Map<String,Object> requestMissingServletRequest(MissingServletRequestParameterException ex){
        ex.printStackTrace();
        Map<String,Object> result = new HashMap<String,Object>();
		result.put(YcContext.CODE, 400);
		result.put(YcContext.MESSAGE, ReturnFormat.retParam(400, null));
		result.put(YcContext.DATA,YcContext.EMPTY_STRING);
		logger.error(ex);
		return result;
    }
    //405错误
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    @ResponseBody
    public Map<String,Object> request405(){
        Map<String,Object> result = new HashMap<String,Object>();
		result.put(YcContext.CODE, 400);
		result.put(YcContext.MESSAGE, ReturnFormat.retParam(405, null));
		result.put(YcContext.DATA,YcContext.EMPTY_STRING);
		logger.error("HttpRequestMethodNotSupportedException================");
		return result;
    }
    //406错误
    @ExceptionHandler({HttpMediaTypeNotAcceptableException.class})
    @ResponseBody
    public Map<String,Object> request406(){
        Map<String,Object> result = new HashMap<String,Object>();
		result.put(YcContext.CODE, 400);
		result.put(YcContext.MESSAGE, ReturnFormat.retParam(406, null));
		result.put(YcContext.DATA,YcContext.EMPTY_STRING);
		logger.error("HttpMediaTypeNotAcceptableException================");
		return result;
    }
    //500错误
    @ExceptionHandler({ConversionNotSupportedException.class,HttpMessageNotWritableException.class})
    @ResponseBody
    public Map<String,Object> server500(RuntimeException runtimeException){
        Map<String,Object> result = new HashMap<String,Object>();
		result.put(YcContext.CODE, 500);
		result.put(YcContext.MESSAGE, ReturnFormat.retParam(406, null));
		result.put(YcContext.DATA,YcContext.EMPTY_STRING);
		logger.error(runtimeException);
		return result;
        
    }
	
}
